package app.qrcode.browser.hearttoheart.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import app.qrcode.browser.hearttoheart.R
import app.qrcode.browser.hearttoheart.adapters.*
import app.qrcode.browser.hearttoheart.databinding.FragmentStudyBinding
import kotlinx.android.synthetic.main.fragment_study.*

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

/**
 * A simple [Fragment] subclass.
 * Use the [StudyFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class StudyFragment : Fragment() {
    // TODO: Rename and change types of parameters

    private var _binding: FragmentStudyBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    private val liveAdapter by lazy { LiveAdapter() }

    private val speechAdapter by lazy { SpeechAdapter() }

    private val videosAdapter by lazy { VideosAdapter() }

    private val liveTeacherList = ArrayList<Live>()

    private val speechTeacherList = ArrayList<Speech>()

    private val videoTeacherList = ArrayList<Video>()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        _binding = FragmentStudyBinding.inflate(inflater, container, false)
        val root: View = binding.root
        initRecyclerview()
        return root
    }

    private fun initRecyclerview() {
        initLiveListRV()
        initSpeechListRV()
        initVideoListRV()
    }

    private fun initLiveListRV() {
        val liveLayoutManager = LinearLayoutManager(context)
        liveLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        binding.liveList.layoutManager = liveLayoutManager
        binding.liveList.adapter = liveAdapter
        binding.liveList.addItemDecoration(LinearSpacingItemDecoration(55))
        liveTeacherList.add(
            Live(
                "杨教授",
                "心理学讲解",
                R.mipmap.head1,
                "直播中",
                "4.6万人在看",
                R.mipmap.pinklivebg
            )
        )
        liveTeacherList.add(
            Live(
                "张教授",
                "教育学讲解",
                R.mipmap.head2,
                "直播中",
                "1.6万人在看",
                R.mipmap.yellowlivebg
            )
        )
        liveTeacherList.add(
            Live(
                "李教授",
                "生理学讲解",
                R.mipmap.head3,
                "直播中",
                "2.3万人在看",
                R.mipmap.bluelivebg
            )
        )
        liveTeacherList.add(
            Live(
                "赵老师",
                "运动学讲解",
                R.mipmap.head5,
                "直播中",
                "5.4万人在看",
                R.mipmap.yellowlivebg
            )
        )
        liveAdapter.updateList(liveTeacherList)
    }

    private fun initSpeechListRV() {
        val speechLayoutManager =
            StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL)
        binding.speechList.layoutManager = speechLayoutManager
        binding.speechList.adapter = speechAdapter
        binding.speechList.addItemDecoration(LinearSpacingItemDecoration(20))
        speechTeacherList.add(Speech("李老师", "人生不是一支短短的蜡烛", R.mipmap.one))
        speechTeacherList.add(Speech("柯老师", "真相只有一个", R.mipmap.two))
        speechTeacherList.add(Speech("杨老师", "生活不止眼前的苟且，还有诗和远方", R.mipmap.three))
        speechTeacherList.add(Speech("郭老师", "生活就像海洋，只有意志坚强的人才能到达彼岸", R.mipmap.picfour))
        speechTeacherList.add(Speech("雷老师", "第一次见你的时候，我的心里已经炸成了烟花", R.mipmap.picfive))
        speechAdapter.updateList(speechTeacherList)
    }

    private fun initVideoListRV() {
        val videosLayoutManager =
            StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL)
        binding.videoList.layoutManager = videosLayoutManager
        binding.videoList.adapter = videosAdapter
        binding.videoList.addItemDecoration(LinearSpacingItemDecoration(20))
        videoTeacherList.add(Video("如何提升教学技能", "19.2万", R.mipmap.picteach))
        videoTeacherList.add(Video("学业知识的拓展", "13.7万", R.mipmap.picteach2))
        videoTeacherList.add(Video("安卓启蒙编程", "12.5万", R.mipmap.android1))
        videoTeacherList.add(Video("篮球大师课", "29.2万", R.mipmap.picbasketball))
        videoTeacherList.add(Video("鲁迅文学大赏", "9.2万", R.mipmap.picluxun))
        videosAdapter.updateList(videoTeacherList)
    }


}